package top.catchcat.blogs.controller;

import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import top.catchcat.blogs.dao.blogs.Blog;
import top.catchcat.blogs.dao.userIfo.User;
import top.catchcat.blogs.service.impl.BlogServiceImpl;

import javax.servlet.http.HttpServletRequest;

@Controller
public class BlogsController {

    @Autowired
    private BlogServiceImpl blogService;

    @GetMapping("/publish/{id}")
    public String updateQuestion(@PathVariable Long id, Model model){
        Blog updateBlog = blogService.getBlogById(id,false);
        model.addAttribute("blog", updateBlog);
        System.out.println("修改"+id);
        return "admin/publish";
    }

    @GetMapping("/blogs/delete/{id}")
    public String deleteQuestion(@PathVariable Long id){
        System.out.println("删除"+id);
        blogService.deleteBlog(id);
        return "redirect:/manger/blogs";
    }

    @GetMapping("/publish")
    public String publishBlog(Model model) {
        if (model.getAttribute("blog") == null) model.addAttribute("blog", new Blog());
        return "admin/publish";
    }

    @PostMapping("/publish") //新增blog
    public String doPublish(Blog blog, HttpServletRequest request, Model model) {
        model.addAttribute("blog", blog);
        String title = blog.getTitle();
        String content = blog.getContent();
        String description = blog.getDescription();

        User user = (User) request.getSession().getAttribute("user");
        if (user == null) {
            model.addAttribute("error", "用户未登录");
            return "publish";
        }
        if (title.equals("") || title == null) {
            model.addAttribute("error", "请输入Blog标题");
            return "publish";
        }

        if (description.equals("") || description == null) {
            model.addAttribute("error", "请输入Blog描述");
            return "publish";
        }

        if (content.equals("") || content == null) {
            model.addAttribute("error", "请输入Blog内容");
            return "publish";
        }


        Blog resultBlog = new Blog();
        resultBlog.setTitle(title);
        resultBlog.setContent(content);
        resultBlog.setTags(blog.getTags());
        resultBlog.setDescription(description);
        resultBlog.setUser_id(user.getId());
        resultBlog.setFirstPicture("");//////////
        resultBlog.setPublished(blog.isPublished());
        System.out.println(resultBlog);
        blogService.insertOrUpdateBlog(resultBlog);
        return "redirect:/";
    }

    @GetMapping("/manger/{action}") //我的blog上下页刷新
    public String mangerBlogs(Model model, HttpServletRequest request,
                              @PathVariable("action") String action,
                              @RequestParam(value = "page", defaultValue = "1") int page) {
        User user = (User) request.getSession().getAttribute("user");
        if (user == null) return "redirect:/";
        else if ("blogs".equals(action)) {
            PageInfo<Blog> allBlogs = blogService.getAllBlogsByUserId(page, 8, user.getId());
            model.addAttribute("allBlogs", allBlogs);
            model.addAttribute("section", "blogs");
            model.addAttribute("sectionName", "我的Blog");
            return "admin/manger";
        } else if ("replies".equals(action)) {
            PageInfo<Blog> allBlogs = blogService.getAllBlogsByUserId(page, 8, user.getId());
            model.addAttribute("allBlogs", allBlogs);
//            model.addAttribute("allReplies", null);
            model.addAttribute("section", "replies");
            model.addAttribute("sectionName", "最新回复");
            return "admin/manger";
        }
        return "redirect:/";
    }
}
